package com.lwj.project1.PersonActivity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Button;

import android.widget.ListView;

import com.lwj.project1.AddGameActivity;

import com.lwj.project1.R;
import com.lwj.project1.RecommendPageFragments.MyGameAdapter;
import com.lwj.project1.databaseHelper.MySqliteOpenHelper;
import com.lwj.project1.entity.Game;
import com.lwj.project1.entity.GameCard;
import com.lwj.project1.entity.GameCategory;

import java.util.ArrayList;
import java.util.List;

public class StoreHouseActivity extends AppCompatActivity {

    private ListView listView;
    private List<Game> games;
    private List<GameCard> gameCards;
    private MySqliteOpenHelper mySqliteOpenHelper;
    private Button add;
    private Button delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_store_house);

        add = findViewById(R.id.button8);
        delete = findViewById(R.id.button10);
        listView = findViewById(R.id.storehouse_listView);
        gameCards = new ArrayList<>();
        mySqliteOpenHelper = new MySqliteOpenHelper(getApplicationContext());
        SharedPreferences sharedPreferences = getSharedPreferences("project_content",MODE_PRIVATE);

        games = mySqliteOpenHelper.findPersonalGame(sharedPreferences.getString("username",""));
        GameCard gameCard;
        if (games == null || games.size() < 0){
            gameCard = new GameCard("","您还没有添加桌游哦~","您还没有添加桌游哦~");
            gameCards.add(gameCard);
            MyGameAdapter adapter = new MyGameAdapter(getApplicationContext(),gameCards);
            listView.setAdapter(adapter);
            return;
        }
        for (int i = 0;i < games.size();i++){
            GameCategory gameCategory = mySqliteOpenHelper.findGameCategoryById(games.get(i).getGameCategoryId());
            gameCard = new GameCard(gameCategory.getGameCategoryImg(),gameCategory.getGameCategoryName(),games.get(i).getGameDescription());
            gameCards.add(gameCard);
        }
        MyGameAdapter adapter = new MyGameAdapter(getApplicationContext(),gameCards);
        listView.setAdapter(adapter);
        add.setOnClickListener(v -> {
            Intent intent = new Intent(StoreHouseActivity.this, AddGameActivity.class);
            startActivity(intent);
        });
    }
}